﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BioBooking.Data;
using System.ComponentModel;

namespace BioBooking.Client.Screens
{
    /// <summary>
    /// Interaction logic for TodaysShowings.xaml
    /// </summary>
    public partial class ListAllMovies : UserControl
    {
        #region Event
        public event SelectionChangedEventHandler SelectionChanged;
        protected virtual void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (SelectionChanged != null)
            {
                SelectionChanged(sender, e);
            }
        }

        public Movie SelectedMovie
        {
            get { return lbTodaysMovies.SelectedItem as Movie; }
        }

        private void lbTodaysShowings_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            OnSelectionChanged(sender, e);
        } 
        #endregion
        IRepository repository;
        ObservableCollection<Movie> movies;
        ObservableCollection<Genre> genres;
        ICollectionView collectionView;
        public ListAllMovies()
        {
            InitializeComponent();
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                repository = Repository.GetRepository();
                movies = new ObservableCollection<Movie>(repository.ListAllActiveMovies());

                collectionView = CollectionViewSource.GetDefaultView(movies);
                collectionView.Filter = new Predicate<object>(FilterCollectionQuery);

                lbTodaysMovies.DataContext = collectionView;
                genres = new ObservableCollection<Genre>(repository.GetAllGenres().OrderBy(p=>p.Title));
                cbGenre.DataContext = genres;

                genres.Insert(0, new Genre() { Title = "Alle Genre" });
                cbGenre.SelectedIndex = 0;
            }
        }

        public bool FilterCollectionQuery(object item)
        {
            Movie movie = item as Movie;

            if (movie == null)
                return false;

            bool titleFound = movie.Titel.ToLower().Contains(txtSearchQuery.Text.ToLower()) || txtSearchQuery.Text.Trim().Length == 0;
            if (cbGenre.SelectedIndex == 0)
            {
                return titleFound;
            }

            if (cbGenre.SelectedIndex > 0)
            {
                return titleFound && movie.Genre == cbGenre.SelectedItem as Genre;
            }

            return true;
        }

        private void btnApplyFilter_Click(object sender, RoutedEventArgs e)
        {
            collectionView.Refresh();
        }

        private void txtSearchQuery_TextChanged(object sender, TextChangedEventArgs e)
        {
            collectionView.Refresh();
        }

        private void cbGenre_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            collectionView.Refresh();
        }
    }
}
